<?php
namespace app\common\model;

// use think\Model;

class Equipment extends \app\common\model\Base
{




    //系统支持自动写入创建和更新的时间戳字段
    protected $autoWriteTimestamp = true; // create_time update_time

    //类型自动转换
    protected $type = [
        'id'    =>  'integer',           // 规则id,自增主键           
    ];



    protected static function init()
    {
        
        //添加设备 后  初始化 设备印章位置表 
        Equipment::event('after_insert', function ($ts) {
            $sealpos = model('Sealpos');

            $num = $ts['equipment_seal_num'];
            $equipment_id = $ts['id'];
            $list = [];
            for($i = 1 ; $i<= $num ; $i++){
                $list[] = ['equipment_id'=>$equipment_id , 'seal_position'=>$i ,'seal_parameter'=>''  ];
            };
            $rs = $sealpos->saveAll($list);
            if(false === $rs){
                $ts->error = $sealpos->getError() ;
                return false;
            }
            return true;
            
        });
    }





    // 数据完成 时间
    protected $auto = array('equipment_status', 'equipment_type', 'equipment_seal_num','equipment_app_version','sort');
    protected $insert = array(); 
    protected $update = [];  
    
    // 数据完成
    protected function setEquipmentStatusAttr($value){
        return 1;
    }
    protected function setEquipmentTypeAttr($value){
        return 1001;
    }
    protected function setEquipmentSealNumAttr($value){
        return 4;
    }
    protected function setEquipmenAppVersionAttr($value){
        return 2.0;
    }
    protected function setSortAttr($value){
        return isset( $value ) ? $value : 50 ;
    }


    // 获取器
    public function getEquipmentStatusTextAttr($value,$data)
    {
        $arr = [-1=>'禁用',1=>'正常'];
        return isset( $arr[$data['equipment_status']] ) ? $arr[$data['equipment_status']]: '未知-' . $data['equipment_status'] ;
    }




    //印章机器 绑定设备
    public function eqSaveData($post){
        if(!array_key_exists('UniqueId', $post )){
            //return ['code'=>1101, 'msg'=> 'UniqueId 不存在!'];
            return 'UniqueId 不存在';
        }

        if(!array_key_exists('type', $post )){
            return '印章类型 不存在';
        }

        if(!array_key_exists('seal_num', $post )){
            return '印章数量 不存在';
        }
        if(!array_key_exists('app_version', $post )){
            return 'app版本号 不存在';
        }


        $map = array();
        $map['equipment_code'] = $post['UniqueId'];
        $equi = $this->where($map)->find();



        //如果没有找到，添加 
        if(!$equi){
            $data = array();
            $data['equipment_code'] = $post['UniqueId'];
            $data['equipment_name'] = '新设备'.date('Y-m-d H:i:s');
            $data['equipment_address'] = '新设备安装地址';
            $data['equipment_info'] = '新设备简介';
            $data['equipment_type'] = $post['type'];
            $data['equipment_seal_num'] = $post['seal_num'];
            $data['equipment_app_version'] = $post['app_version'];


            $rs = $this->isUpdate(false)->save($data);

            $equi = $this;
        }

        return ['code'=>0,'msg'=>'ok','data'=>$equi];
        return 'lrxin';
    }
	


}